#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <functional>
#include <limits>
#include <iomanip>

using namespace std;

using int64 = long long;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    const double PI = 3.1415926;
    while (T--)
    {
        long long a, b, c, d;
        cin >> a >> b >> c >> d;
        long long N = c * d;
        long long ac = a * c;

        long long ans = 0;

        // 枚举 N 的约数
        for (long long i = 1; i * i <= N; i++)
        {
            if (N % i == 0)
            {
                // i 是约数
                vector<long long> cand = {i, N / i};
                for (long long m : cand)
                {
                    if (m <= ac)
                        continue;
                    long long diff = m - ac;
                    if (diff % b != 0)
                        continue;
                    long long x = diff / b;
                    if (x <= 0)
                        continue;
                    long long num = N * x;
                    if (num % m != 0)
                        continue;
                    long long y = num / m;
                    if (y <= 0)
                        continue;
                    ans++;
                }
            }
        }
        cout << ans << "\n";
    }

    return 0;
}
